package com.qf.shop.controller;

import com.qf.shop.entity.User;
import com.qf.shop.service.UserService;
import com.qf.shop.service.impl.UserServiceImpl;
import com.qf.shop.utils.Base64Utils;
import com.qf.shop.utils.Constants;
import com.qf.shop.utils.EmailUtils;
import com.qf.shop.utils.RandomUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/user")
public class UserController extends BaseController {
    //验证用户名
    public String check(HttpServletRequest request, HttpServletResponse response) {
        //1.收参
        String username = request.getParameter("username");
        //2.调用业务逻辑  验证用户名是否存在
        UserService userService = new UserServiceImpl();
        User user = userService.checkUsername(username);
        //3.根据业务返回的结果，执行响应的操作  打印状态码
        if (user == null) {
            return "0";
        } else {
            return "1";
        }
    }

    //注册
    public String register(HttpServletRequest request, HttpServletResponse response) {
        //1.收参
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        //2.封装实体类对象  权限、激活码状态都得是初始化状态
        User user = new User(username, password, email, gender, Constants.USER_NOT_ACTIVE, Constants.USER_CUSTOMER, RandomUtils.createActive());
        //3.调用业务逻辑 根据返回的结果 ，跳转registerSuccess.jsp,
        //选择激活或者返回主页
        UserService userService = new UserServiceImpl();
        int result = userService.register(user);
        if (result == 0) {
            request.setAttribute("registerMsg", "注册失败！");
            return Constants.FORWARD+"/register.jsp";
        } else {
            EmailUtils.sendEmail(user);//发送激活邮件
            return Constants.REDIRECT + "/registerSuccess.jsp";
        }

    }
    //激活
    public  String active(HttpServletRequest request,HttpServletResponse response){
        //1.收参
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        //2.解码
        email= Base64Utils.decode(email);//
        code= Base64Utils.decode(code);
        //3.调用业务层执行修改  根据email  +code
        UserService userService = new UserServiceImpl();
        int i = userService.activeUser(email,code);
        //4.跳转到message.jsp页面 ，并打印注册结果
        if(i>0){
            request.setAttribute("msg","激活成功！");
        }else{
            request.setAttribute("msg","激活失败！");
        }
        return Constants.FORWARD+"/message.jsp";
    }
   //登录
    public String login(HttpServletRequest request,HttpServletResponse response){
        //1.收参
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String  auto = request.getParameter("auto");

        //2.调用业务验证用户名或密码是否存在
        UserService userService = new UserServiceImpl();
        //header.jsp 里面叫loginUser所以这里接收的变量名叫这个
        User loginUSer = userService.loginUser(username,password);

        //3.根据结果  判断存在作用域的是登录对象还是登录失败的信息
        if(loginUSer!=null&&loginUSer.getFlag()==1){
            request.getSession().setAttribute("loginUser",loginUSer);
            if(auto!=null){//勾选了自动登录
                Cookie cookie = new Cookie("userInfo", Base64Utils.encode(username+"#"+password));
                cookie.setPath("/");
                cookie.setMaxAge(60*60*24*14);//最大保存时间
                response.addCookie(cookie);
            }
            return Constants.REDIRECT+"index.jsp";
        }else{
            request.setAttribute("msg", "用户名或密码错误或者未激活！");
            return Constants.FORWARD+"/login.jsp";
        }
    }
    //注销
    public String logOut(HttpServletRequest request,HttpServletResponse response){
        //1.清空session
        request.getSession().removeAttribute("loginUser");
        //2.清空Cookie
        Cookie cookie = new Cookie("userInfo", "");
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        request.setAttribute("msg", "注销成功！");
        return Constants.FORWARD+"/login.jsp";



    }

}